package com.eshop.modules.shop.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;
import org.springframework.web.bind.annotation.PutMapping;
import com.eshop.modules.aop.ForbidSubmit;
import com.eshop.modules.shop.domain.SystemStore;
import org.springframework.web.bind.annotation.PostMapping;
import com.alibaba.fastjson.JSONObject;
import com.eshop.exception.BadRequestException;
import com.eshop.utils.location.LocationUtils;
import com.alibaba.fastjson.JSON;
import com.eshop.utils.location.GetTencentLocationVO;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import org.springframework.data.domain.Pageable;
import java.io.IOException;
import com.eshop.modules.shop.service.dto.SystemStoreDto;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.eshop.modules.shop.service.dto.SystemStoreQueryCriteria;
import com.eshop.dozer.service.IGenerator;
import com.eshop.modules.shop.service.SystemStoreService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "门店管理" })
@RestController
@RequestMapping({ "/api/yxSystemStore" })
public class SystemStoreController
{
    private final SystemStoreService systemStoreService;
    private final IGenerator generator;
    
    public SystemStoreController(final SystemStoreService systemStoreService, final IGenerator generator) {
        this.systemStoreService = systemStoreService;
        this.generator = generator;
    }
    
    @Log("所有门店")
    @ApiOperation("所有门店")
    @GetMapping({ "/all" })
    @PreAuthorize("@el.check('yxSystemStore:list')")
    public ResponseEntity<Object> getAll(final SystemStoreQueryCriteria criteria) {
        return new ResponseEntity(this.systemStoreService.queryAll(criteria), HttpStatus.OK);
    }
    
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('yxSystemStore:list')")
    public void download(final HttpServletResponse response, final SystemStoreQueryCriteria criteria) throws IOException {
        this.systemStoreService.download(this.generator.convert(this.systemStoreService.queryAll(criteria), (Class)SystemStoreDto.class), response);
    }
    
    @GetMapping
    @Log("查询门店")
    @ApiOperation("查询门店")
    @PreAuthorize("@el.check('yxSystemStore:list')")
    public ResponseEntity<Object> getYxSystemStores(final SystemStoreQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.systemStoreService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @PostMapping({ "/getL" })
    @Log("获取经纬度")
    @ApiOperation("获取经纬度")
    @PreAuthorize("@el.check('yxSystemStore:getl')")
    public ResponseEntity<GetTencentLocationVO> create(@Validated @RequestBody final String jsonStr) {
        final JSONObject jsonObject = JSON.parseObject(jsonStr);
        final String addr = jsonObject.getString("addr");
        final GetTencentLocationVO locationVO = LocationUtils.getLocation(addr);
        if (locationVO.getStatus() != 0) {
            throw new BadRequestException(locationVO.getMessage());
        }
        return (ResponseEntity<GetTencentLocationVO>)new ResponseEntity(locationVO, HttpStatus.CREATED);
    }
    
    @ForbidSubmit
    @PostMapping
    @Log("新增门店")
    @ApiOperation("新增门店")
    @PreAuthorize("@el.check('yxSystemStore:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody final SystemStore resources) {
        return new ResponseEntity(this.systemStoreService.save(resources), HttpStatus.CREATED);
    }
    
    @ForbidSubmit
    @PutMapping
    @Log("修改门店")
    @ApiOperation("修改门店")
    @PreAuthorize("@el.check('yxSystemStore:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody final SystemStore resources) {
        this.systemStoreService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除门店")
    @ApiOperation("删除门店")
    @PreAuthorize("@el.check('yxSystemStore:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody final Integer[] ids) {
        this.systemStoreService.removeByIds((Collection)new ArrayList(Arrays.asList(ids)));
        return new ResponseEntity(HttpStatus.OK);
    }
}
